package com.ruoyi.pay.service.impl;

import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.pay.domain.PayCoin;
import com.ruoyi.pay.domain.vo.PayCoinVO;
import com.ruoyi.pay.domain.vo.PayOrderVO;
import com.ruoyi.pay.mapper.PayCoinMapper;
import com.ruoyi.pay.service.IPayCoinService;
import com.ruoyi.pay.service.IPayOrderService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wx.domain.WxPayReturnInfo;

/**
 * 充值Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-05
 */
@Service
public class PayCoinServiceImpl extends ServiceImpl<PayCoinMapper, PayCoin> implements IPayCoinService {
    
	@Autowired
    private PayCoinMapper payCoinMapper;
	
	@Autowired
	private IPayOrderService payOrderService;
	
	@Autowired
	private ISysUserService sysUserService;

    /**
     * 查询充值
     * 
     * @param id 充值主键
     * @return 充值
     */
    @Override
    public PayCoin selectPayCoinById(String id)
    {
        return payCoinMapper.selectPayCoinById(id);
    }

    /**
     * 查询充值列表
     * 
     * @param payCoin 充值
     * @return 充值
     */
    @Override
    public List<PayCoin> selectPayCoinList(PayCoin payCoin)
    {
        return payCoinMapper.selectPayCoinList(payCoin);
    }

    /**
     * 新增充值
     * 
     * @param payCoin 充值
     * @return 结果
     */
    @Override
    public int insertPayCoin(PayCoin payCoin)
    {
        payCoin.setCreateTime(DateUtils.getNowDate());
        return payCoinMapper.insertPayCoin(payCoin);
    }

    /**
     * 修改充值
     * 
     * @param payCoin 充值
     * @return 结果
     */
    @Override
    public int updatePayCoin(PayCoin payCoin)
    {
        payCoin.setUpdateTime(DateUtils.getNowDate());
        return payCoinMapper.updatePayCoin(payCoin);
    }

    /**
     * 批量删除充值
     * 
     * @param ids 需要删除的充值主键
     * @return 结果
     */
    @Override
    public int deletePayCoinByIds(String[] ids)
    {
        return payCoinMapper.deletePayCoinByIds(ids);
    }

    /**
     * 删除充值信息
     * 
     * @param id 充值主键
     * @return 结果
     */
    @Override
    public int deletePayCoinById(String id)
    {
        return payCoinMapper.deletePayCoinById(id);
    }

	@Override
	public WxPayReturnInfo pay(PayCoinVO payCoin) {
		PayCoin coin = payCoinMapper.selectById(payCoin.getId());
		PayOrderVO payOrder = new PayOrderVO();
		payOrder.setAppId(payCoin.getAppId());
		payOrder.setAmount(coin.getAmount());
		payOrder.setCoinNum(coin.getCoinNum());
		payOrder.setBody("会员充值");
		return payOrderService.createPayOrder(payOrder);
	}

	@Override
	public WxPayReturnInfo payNum(PayCoinVO payCoin) {
		if(payCoin.getCoinNum().intValue() == 200){
			payCoin.setCoinNum(100);
		}
		SysUser sysUser = sysUserService.getById(SecurityUtils.getUserId());
		PayOrderVO payOrder = new PayOrderVO();
		payOrder.setAppId(payCoin.getAppId());
		payOrder.setAmount(BigDecimal.valueOf(payCoin.getCoinNum()).divide(BigDecimal.valueOf(100)));
		payOrder.setCoinNum(payCoin.getCoinNum());
		if(sysUser.getVisitUserId() == null){
			payOrder.setBody("会员充值");
		}else{
			SysUser visitor = sysUserService.getById(sysUser.getVisitUserId());
			payOrder.setBody("会员充值"+visitor.getFriendId());
		}
		return payOrderService.createPayOrder(payOrder);
	}

	@Override
	public WxPayReturnInfo payVip(PayCoinVO payCoin) {
		SysUser sysUser = sysUserService.getById(SecurityUtils.getUserId());
		PayOrderVO payOrder = new PayOrderVO();
		payOrder.setAppId(payCoin.getAppId());
		payOrder.setAmount(BigDecimal.valueOf(payCoin.getCoinNum()).divide(BigDecimal.valueOf(100)));
		payOrder.setCoinNum(payCoin.getCoinNum());
		if(sysUser.getVisitUserId() == null){
			payOrder.setBody("会员充值");
		}else{
			SysUser visitor = sysUserService.getById(sysUser.getVisitUserId());
			payOrder.setBody("会员充值"+visitor.getFriendId());
		}
		return payOrderService.createVipPayOrder(payOrder);
	}

	@Override
	public WxPayReturnInfo payStoreNum(PayCoinVO payCoin) {
		if(payCoin.getCoinNum().intValue() == 200){
			payCoin.setCoinNum(100);
		}
		SysUser sysUser = sysUserService.getById(SecurityUtils.getUserId());
		if(payCoin.getCoinNum() < 10){
			throw new ServiceException("最低支付0.1元");
		}
		PayOrderVO payOrder = new PayOrderVO();
		payOrder.setAppId(payCoin.getAppId());
		payOrder.setAmount(BigDecimal.valueOf(payCoin.getCoinNum()).divide(BigDecimal.valueOf(100)));
		payOrder.setCoinNum(payCoin.getCoinNum());
		if(sysUser.getVisitUserId() == null){
			payOrder.setBody("购物充值");
		}else{
			SysUser visitor = sysUserService.getById(sysUser.getVisitUserId());
			payOrder.setBody("购物充值"+visitor.getFriendId());
		}
		return payOrderService.createStorePayOrder(payOrder);
	}
	
}
